// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Author: wsfuyibing <682805@qq.com>
// Date: 2024-12-16

package errs

import "fmt"

// builder
// 构建错误.
type builder struct {
	code    int
	message string
}

// NewError
// 创建错误实例.
//
//   runtime.NewError(400, "Bad request")
//   runtime.NewError(401, "Unauthorized")
//   runtime.NewError(403, "Forbidden")
func NewError(code int, format string, args ...any) Error {
	return &builder{
		code:    code,
		message: fmt.Sprintf(format, args...),
	}
}

// Code
// 错误码.
//
//   return 400
//   return 401
//   return 403
func (o *builder) Code() int {
	return o.code
}

// Error
// 错误信息.
//   return "400: Bad Request"
//   return "401: Unauthorized"
//   return "403: Forbidden"
func (o *builder) Error() string {
	return fmt.Sprintf(`%d: %s`, o.code, o.message)
}

// Message
// 错误信息.
//
//   return "Bad Request"
//   return "Unauthorized"
//   return "Forbidden"
func (o *builder) Message() string {
	return o.message
}
